/*
 * A V4L2 driver for ov5640_mipi YUV cameras.
 *
 * Copyright (c) 2017 by Allwinnertech Co., Ltd.  http://www.allwinnertech.com
 *
 * Authors:  Sun Xilong <sunxilong@allwinnertech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/videodev2.h>
#include <linux/clk.h>
#include <media/v4l2-device.h>
#include <media/v4l2-mediabus.h>
#include <linux/io.h>

#include "camera.h"
#include "sensor_helper.h"

MODULE_AUTHOR("sunxilong");
MODULE_DESCRIPTION("A low-level driver for ov5640_mipi sensors");
MODULE_LICENSE("GPL");

#define AF_WIN_NEW_COORD

#define MCLK              (24*1000*1000)
int MCLK_DIV = 1;
#ifdef CONFIG_ARCH_SUN9IW1P1
int A80_VERSION;
#endif
#define V4L2_IDENT_SENSOR 0x5640
#ifdef _FLASH_FUNC_
#include "../modules/flash/flash.h"
unsigned int to_flash;
static unsigned int flash_auto_level = 0x1c;
#endif
#define CONTINUEOUS_AF

#define DENOISE_LV_AUTO
#define SHARPNESS 0x18
#ifdef AUTO_FPS

#endif

#ifndef DENOISE_LV_AUTO
#define DENOISE_LV 0x8
#endif

#define AE_CW 1
unsigned int night_mode;
unsigned int Nfrms = 1;
unsigned int cap_manual_gain = 0x10;
#define CAP_GAIN_CAL 0
#define CAP_MULTI_FRAMES
#ifdef CAP_MULTI_FRAMES
#define MAX_FRM_CAP 4
#else
#define MAX_FRM_CAP 1
#endif
#define I2C_ADDR 0x78
#define SENSOR_NAME "ov5640_mipi"

struct cfg_array {		/* coming later */
	struct regval_list *regs;
	int size;
};

static struct regval_list sensor_init_regs[] = {
	//YCbCr init parameters
	{0x3103 , 0x11},
	{0x3008 , 0x82},
	//delay 5ms
	{REG_DLY, 0x10},
	{0x3008 , 0x42},
	{0x3103 , 0x03},
	{0x3017 , 0x00},
	{0x3018 , 0x00},
	{0x3034 , 0x18},
	{0x3037 , 0x13},
	{0x3108 , 0x01},
	{0x3630 , 0x36},
	{0x3631 , 0x0e},
	{0x3632 , 0xe2},
	{0x3633 , 0x12},
	{0x3621 , 0xe0},
	{0x3704 , 0xa0},
	{0x3703 , 0x5a},
	{0x3715 , 0x78},
	{0x3717 , 0x01},
	{0x370b , 0x60},
	{0x3705 , 0x1a},
	{0x3905 , 0x02},
	{0x3906 , 0x10},
	{0x3901 , 0x0a},
	{0x3731 , 0x12},
	{0x3600 , 0x08},
	{0x3601 , 0x33},
	{0x302d , 0x60},
	{0x3620 , 0x52},
	{0x371b , 0x20},
	{0x471c , 0x50},
	{0x3a13 , 0x43},
	{0x3a18 , 0x00},
	{0x3a19 , 0xf8},
	{0x3635 , 0x13},
	{0x3636 , 0x03},
	{0x3634 , 0x40},
	{0x3622 , 0x01},

	//50/60Hz
	{0x3c01 , 0x34},
	{0x3c04 , 0x28},
	{0x3c05 , 0x98},
	{0x3c06 , 0x00},
	{0x3c08 , 0x00},
	{0x3c09 , 0x1c},
	{0x3c0a , 0x9c},
	{0x3c0b , 0x40},

	//timing
	{0x3800 , 0x00},
	{0x3801 , 0x00},
	{0x3802 , 0x00},
	{0x3804 , 0x0a},
	{0x3805 , 0x3f},
	{0x3810 , 0x00},
	{0x3811 , 0x10},
	{0x3812 , 0x00},
	{0x3708 , 0x64},
	{0x3a08 , 0x01},
	{0x4001 , 0x02},
	{0x4005 , 0x1a},
	{0x3000 , 0x00},
	{0x3002 , 0x1c},
	{0x3004 , 0xff},
	{0x3006 , 0xc3},
	{0x300e , 0x45},
	{0x302e , 0x08},
	{0x4300 , 0x30},
	{0x501f , 0x00},
	{0x4407 , 0x04},
	{0x440e , 0x00},
	{0x5000 , 0xa7},

	//AWB
	{0x5180 , 0xff},
	{0x5181 , 0xf2},
	{0x5182 , 0x00},
	{0x5183 , 0x14},
	{0x5184 , 0x25},
	{0x5185 , 0x24},
	{0x5186 , 0x09},
	{0x5187 , 0x09},
	{0x5188 , 0x09},
	{0x5189 , 0x75},
	{0x518a , 0x54},
	{0x518b , 0xe0},
	{0x518c , 0xb2},
	{0x518d , 0x42},
	{0x518e , 0x3d},
	{0x518f , 0x56},
	{0x5190 , 0x46},
	{0x5191 , 0xf8},
	{0x5192 , 0x04},
	{0x5193 , 0x70},
	{0x5194 , 0xf0},
	{0x5195 , 0xf0},
	{0x5196 , 0x03},
	{0x5197 , 0x01},
	{0x5198 , 0x04},
	{0x5199 , 0x12},
	{0x519a , 0x04},
	{0x519b , 0x00},
	{0x519c , 0x06},
	{0x519d , 0x82},
	{0x519e , 0x38},

	//color matrix
	{0x5381 , 0x1e},
	{0x5382 , 0x5b},
	{0x5383 , 0x08},
	{0x5384 , 0x0a},
	{0x5385 , 0x7e},
	{0x5386 , 0x88},
	{0x5387 , 0x7c},
	{0x5388 , 0x6c},
	{0x5389 , 0x10},
	{0x538a , 0x01},
	{0x538b , 0x98},
	//CIP
	{0x5300 , 0x08},
	{0x5301 , 0x30},
	{0x5302 , 0x10},
	{0x5303 , 0x00},
	{0x5304 , 0x08},
	{0x5305 , 0x30},
	{0x5306 , 0x08},
	{0x5307 , 0x16},
	{0x5309 , 0x08},
	{0x530a , 0x30},
	{0x530b , 0x04},
	{0x530c , 0x06},

	//gamma
	{0x5480 , 0x01},
	{0x5481 , 0x08},
	{0x5482 , 0x14},
	{0x5483 , 0x28},
	{0x5484 , 0x51},
	{0x5485 , 0x65},
	{0x5486 , 0x71},
	{0x5487 , 0x7d},
	{0x5488 , 0x87},
	{0x5489 , 0x91},
	{0x548a , 0x9a},
	{0x548b , 0xaa},
	{0x548c , 0xb8},
	{0x548d , 0xcd},
	{0x548e , 0xdd},
	{0x548f , 0xea},
	{0x5490 , 0x1d},

	//UV adjust
	{0x5580 , 0x06},
	{0x5583 , 0x40},
	{0x5584 , 0x10},
	{0x5589 , 0x10},
	{0x558a , 0x00},
	{0x558b , 0xf8},
	{0x501d , 0x04},
	//lens correction
	{0x5800 , 0x23},
	{0x5801 , 0x14},
	{0x5802 , 0x0f},
	{0x5803 , 0x0f},
	{0x5804 , 0x12},
	{0x5805 , 0x26},
	{0x5806 , 0x0c},
	{0x5807 , 0x08},
	{0x5808 , 0x05},
	{0x5809 , 0x05},
	{0x580a , 0x08},
	{0x580b , 0x0d},
	{0x580c , 0x08},
	{0x580d , 0x03},
	{0x580e , 0x00},
	{0x580f , 0x00},
	{0x5810 , 0x03},
	{0x5811 , 0x09},
	{0x5812 , 0x07},
	{0x5813 , 0x03},
	{0x5814 , 0x00},
	{0x5815 , 0x01},
	{0x5816 , 0x03},
	{0x5817 , 0x08},
	{0x5818 , 0x0d},
	{0x5819 , 0x08},
	{0x581a , 0x05},
	{0x581b , 0x06},
	{0x581c , 0x08},
	{0x581d , 0x0e},
	{0x581e , 0x29},
	{0x581f , 0x17},
	{0x5820 , 0x11},
	{0x5821 , 0x11},
	{0x5822 , 0x15},
	{0x5823 , 0x28},
	{0x5824 , 0x46},
	{0x5825 , 0x26},
	{0x5826 , 0x08},
	{0x5827 , 0x26},
	{0x5828 , 0x64},
	{0x5829 , 0x26},
	{0x582a , 0x24},
	{0x582b , 0x22},
	{0x582c , 0x24},
	{0x582d , 0x24},
	{0x582e , 0x06},
	{0x582f , 0x22},
	{0x5830 , 0x40},
	{0x5831 , 0x42},
	{0x5832 , 0x24},
	{0x5833 , 0x26},
	{0x5834 , 0x24},
	{0x5835 , 0x22},
	{0x5836 , 0x22},
	{0x5837 , 0x26},
	{0x5838 , 0x44},
	{0x5839 , 0x24},
	{0x583a , 0x26},
	{0x583b , 0x28},
	{0x583c , 0x42},
	{0x583d , 0xce},

	{0x5025 , 0x00},
	{0x3a0f , 0x30},
	{0x3a10 , 0x28},
	{0x3a1b , 0x30},
	{0x3a1e , 0x26},
	{0x3a11 , 0x60},
	{0x3a1f , 0x14},
	{0x3008 , 0x02},
};

static struct regval_list sensor_vga_30fps_regs[] = {
	{0x3035 , 0x14},
	{0x3036 , 0x38},
	{0x3c07 , 0x08},
	{0x3820 , 0x41},
	{0x3821 , 0x07},
	//timing
	{0x3814 , 0x31},
	{0x3815 , 0x31},
	{0x3803 , 0x04},
	{0x3806 , 0x07},
	{0x3807 , 0x9b},
	{0x3808 , 0x02},
	{0x3809 , 0x80},
	{0x380a , 0x01},
	{0x380b , 0xe0},
	{0x380c , 0x07},
	{0x380d , 0x68},
	{0x380e , 0x03},
	{0x380f , 0xd8},
	{0x3813 , 0x06},
	
	{0x3618 , 0x00},
	{0x3612 , 0x29},
	{0x3709 , 0x52},
	{0x370c , 0x03},
	
	{0x3a02 , 0x03},
	{0x3a03 , 0xd8},
	{0x3a09 , 0x27},
	{0x3a0a , 0x00},
	{0x3a0b , 0xf6},
	{0x3a0e , 0x03},
	{0x3a0d , 0x04},
	{0x3a14 , 0x03},
	{0x3a15 , 0xd8},
	
	{0x4004 , 0x02},
	{0x4713 , 0x03},
	{0x460b , 0x35},
	{0x460c , 0x22},
	{0x4837 , 0x44},
	{0x3824 , 0x02},
	{0x5001 , 0xa3},
};

static struct regval_list sensor_720p_60fps_regs[] = {
	// YCbCr 720p video 60fps
	{0x3035 , 0x11},
	{0x3036 , 0x54},
	{0x3c07 , 0x07},
	{0x3820 , 0x41},
	{0x3821 , 0x07},
	//timing
	{0x3814 , 0x31},
	{0x3815 , 0x31},
	{0x3803 , 0xfa},
	{0x3806 , 0x06},
	{0x3807 , 0xa9},
	{0x3808 , 0x05},
	{0x3809 , 0x00},
	{0x380a , 0x02},
	{0x380b , 0xd0},
	{0x380c , 0x07},
	{0x380d , 0x64},
	{0x380e , 0x02},
	{0x380f , 0xe4},
	{0x3813 , 0x04},
	
	{0x3618 , 0x00},
	{0x3612 , 0x29},
	{0x3709 , 0x52},
	{0x370c , 0x03},
	
	//banding filter
	{0x3a02 , 0x02},
	{0x3a03 , 0xe4},
	{0x3a09 , 0xbc},
	{0x3a0a , 0x01},
	{0x3a0b , 0x72},
	{0x3a0e , 0x01},
	{0x3a0d , 0x02},
	{0x3a14 , 0x02},
	{0x3a15 , 0xe4},
	
	{0x4004 , 0x02},
	{0x4713 , 0x02},
	{0x460b , 0x37},
	{0x460c , 0x20},
	{0x4837 , 0x16},
	{0x3824 , 0x04},
	{0x5001 , 0x83},
};
static struct regval_list sensor_5M_15fps_regs[] = {
	{0x3035 , 0x11},
	{0x3036 , 0x54},
	{0x3c07 , 0x07},
	{0x3820 , 0x40},
	{0x3821 , 0x06},
	//timing
	{0x3814 , 0x11},
	{0x3815 , 0x11},
	{0x3803 , 0x00},
	{0x3806 , 0x07},
	{0x3807 , 0x9f},
	{0x3808 , 0x0a},
	{0x3809 , 0x20},
	{0x380a , 0x07},
	{0x380b , 0x98},
	{0x380c , 0x0b},
	{0x380d , 0x1c},
	{0x380e , 0x07},
	{0x380f , 0xb0},
	{0x3813 , 0x04},
	
	{0x3618 , 0x04},
	{0x3612 , 0x2b},
	{0x3709 , 0x12},
	{0x370c , 0x00},
	//banding filter
	{0x3a02 , 0x07},
	{0x3a03 , 0xb0},
	
	{0x3a09 , 0x27},
	{0x3a0a , 0x00},
	{0x3a0b , 0xf6},
	{0x3a0e , 0x06},
	{0x3a0d , 0x08},
	{0x3a14 , 0x07},
	{0x3a15 , 0xb0},
	
	{0x4004 , 0x06},
	{0x4713 , 0x00},
	{0x460b , 0x35},
	{0x460c , 0x22},
	{0x4837 , 0x0a},
	{0x3824 , 0x01},
	{0x5001 , 0x83},
};

#ifdef AUTO_FPS

static struct regval_list sensor_auto_fps_mode[] = {};

static struct regval_list sensor_fix_fps_mode[] = {};
#endif

static struct regval_list sensor_oe_disable_regs[] = {};

static struct regval_list sensor_oe_enable_regs[] = {};

static struct regval_list sensor_sw_stby_on_regs[] = {};

static struct regval_list sensor_sw_stby_off_regs[] = {};

static unsigned char sensor_af_fw_regs[] = {};

//4.10.5 
static struct regval_list sensor_wb_auto_regs[] = {
	{0x3212, 0x03},
	{0x3406, 0x00},
	{0x3400, 0x04},
	{0x3401, 0x00},
	{0x3402, 0x04},
	{0x3403, 0x00},
	{0x3404, 0x04},
	{0x3405, 0x00},
	{0x3212, 0x13},
	{0x3212, 0xa3},
};

static struct regval_list sensor_wb_sunny_regs[] = {
	{0x3212, 0x03},
	{0x3406, 0x01},
	{0x3400, 0x06},
	{0x3401, 0x1c},
	{0x3402, 0x04},
	{0x3403, 0x00},
	{0x3404, 0x04},
	{0x3405, 0xf3},
	{0x3212, 0x13},
	{0x3212, 0xa3},
};

static struct regval_list sensor_wb_office_regs[] = {
	{0x3212, 0x03},
	{0x3406, 0x01},
	{0x3400, 0x05},
	{0x3401, 0x48},
	{0x3402, 0x04},
	{0x3403, 0x00},
	{0x3404, 0x07},
	{0x3405, 0xcf},
	{0x3212, 0x13},
	{0x3212, 0xa3},
};

static struct regval_list sensor_wb_cloudy_regs[] = {
	{0x3212, 0x03},
	{0x3406, 0x01},
	{0x3400, 0x06},
	{0x3401, 0x48},
	{0x3402, 0x04},
	{0x3403, 0x00},
	{0x3404, 0x04},
	{0x3405, 0xd3},
	{0x3212, 0x13},
	{0x3212, 0xa3},
};

static struct regval_list sensor_wb_home_regs[] = {
	{0x3212, 0x03},
	{0x3406, 0x01},
	{0x3400, 0x04},
	{0x3401, 0x10},
	{0x3402, 0x04},
	{0x3403, 0x00},
	{0x3404, 0x08},
	{0x3405, 0x40},
	{0x3212, 0x13},
	{0x3212, 0xa3},
};

static struct cfg_array sensor_wb[] = {
	{
	 .regs = sensor_wb_auto_regs,
	 .size = ARRAY_SIZE(sensor_wb_auto_regs),
	 },
	{
	 .regs = sensor_wb_sunny_regs,
	 .size = ARRAY_SIZE(sensor_wb_sunny_regs),
	 },
	 {
	 .regs = sensor_wb_office_regs,
	 .size = ARRAY_SIZE(sensor_wb_office_regs),
	 },
	 {
	 .regs = sensor_wb_cloudy_regs,
	 .size = ARRAY_SIZE(sensor_wb_cloudy_regs),
	 },
	 {
	 .regs = sensor_wb_home_regs,
	 .size = ARRAY_SIZE(sensor_wb_home_regs),
	 },
};
 //4.10.6 
 static struct regval_list sensor_colorfx_none_regs[] = {
	 //Normal(off)
	 {0x3212, 0x03},
	 {0x5580, 0x06},
	 {0x5583, 0x40},
	 {0x5584, 0x10},
	 {0x5003, 0x08},
	 {0x3212, 0x13},
	 {0x3212, 0xa3},
 };
 static struct regval_list sensor_colorfx_sky_blue_regs[] = {
	 //Blueish(cool light)
	 {0x3212, 0x03},
	 {0x5580, 0x1e},
	 {0x5583, 0xa0},
	 {0x5584, 0x40},
	 {0x5003, 0x08},
	 {0x3212, 0x13},
	 {0x3212, 0xa3},
 };
 static struct regval_list sensor_colorfx_emboss_regs[] = {
	 //Redish(warm)
	 {0x3212, 0x03},
	 {0x5580, 0x1e},
	 {0x5583, 0x80},
	 {0x5584, 0xc0},
	 {0x5003, 0x08},
	 {0x3212, 0x13},
	 {0x3212, 0xa3},
 };
 
 static struct regval_list sensor_colorfx_bw_regs[] = {
	 //Black and white
	 {0x3212, 0x03},
	 {0x5580, 0x1e},
	 {0x5583, 0x80},
	 {0x5584, 0x80},
	 {0x5003, 0x08},
	 {0x3212, 0x13},
	 {0x3212, 0xa3},
 };
 
 static struct regval_list sensor_colorfx_sepia_regs[] = {
	 //Sepia
	 {0x3212, 0x03},
	 {0x5580, 0x1e},
	 {0x5583, 0x40},
	 {0x5584, 0xa0},
	 {0x5003, 0x08},
	 {0x3212, 0x13},
	 {0x3212, 0xa3},
 };
 
 static struct regval_list sensor_colorfx_negative_regs[] = {
	 //Negative
	 {0x3212, 0x03},
	 {0x5580, 0x40},
	 {0x5003, 0x08},
	 {0x5583, 0x40},
	 {0x5584, 0x10},
	 {0x3212, 0x13},
	 {0x3212, 0xa3},
 };
 
 static struct regval_list sensor_colorfx_grass_green_regs[] = {
	 //Greenish
	 {0x3212, 0x03},
	 {0x5580, 0x1e},
	 {0x5583, 0x60},
	 {0x5584, 0x60},
	 {0x5003, 0x08},
	 {0x3212, 0x13},
	 {0x3212, 0xa3},
 };
 
 
 static struct regval_list sensor_colorfx_over_exposure_regs[] = {
	 //Overexposure
	 {0x3212, 0x03},
	 {0x5580, 0x1e},
	 {0x5583, 0xf0},
	 {0x5584, 0xf0},
	 {0x5003, 0x08},
	 {0x3212, 0x13},
	 {0x3212, 0xa3},
 };
 
 static struct regval_list sensor_colorfx_solarization_regs[] = {
	 //Solarize
	 {0x3212, 0x03},
	 {0x5580, 0x06},
	 {0x5583, 0x40},
	 {0x5584, 0x10},
	 {0x5003, 0x09},
	 {0x3212, 0x13},
	 {0x3212, 0xa3},
 };
 
static struct cfg_array sensor_colorfx[] = {
	 {
	  .regs = sensor_colorfx_none_regs,
	  .size = ARRAY_SIZE(sensor_colorfx_none_regs),
	  },
	 {
	  .regs = sensor_colorfx_sky_blue_regs,
	  .size = ARRAY_SIZE(sensor_colorfx_sky_blue_regs),
	  },
	 {
	  .regs = sensor_colorfx_emboss_regs,
	  .size = ARRAY_SIZE(sensor_colorfx_emboss_regs),
	  },
	 {
	  .regs = sensor_colorfx_bw_regs,
	  .size = ARRAY_SIZE(sensor_colorfx_bw_regs),
	  },
	 {
	  .regs = sensor_colorfx_sepia_regs,
	  .size = ARRAY_SIZE(sensor_colorfx_sepia_regs),
	  },
	 {
	  .regs = sensor_colorfx_negative_regs,
	  .size = ARRAY_SIZE(sensor_colorfx_negative_regs),
	  },
	 {
	  .regs = sensor_colorfx_grass_green_regs,
	  .size = ARRAY_SIZE(sensor_colorfx_grass_green_regs),
	  },
	 {
	  .regs = sensor_colorfx_over_exposure_regs,
	  .size = ARRAY_SIZE(sensor_colorfx_over_exposure_regs),
	  },
	  {
	  .regs = sensor_colorfx_solarization_regs,
	  .size = ARRAY_SIZE(sensor_colorfx_solarization_regs),
	  },
 };

#if 1
 static struct regval_list sensor_sharpness_auto_regs[] = {};
#endif
#if 1
 static struct regval_list sensor_denoise_auto_regs[] = {};
#endif


 //4.10.1 brightness
 static struct regval_list sensor_brightness_neg4_regs[] = {
	 //brightness -4
	 {0x3212 , 0x03},
	 {0x5587 , 0x40},
	 {0x5588 , 0x09},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_brightness_neg3_regs[] = {
	 //brightness -3
	 {0x3212 , 0x03},
	 {0x5587 , 0x30},
	 {0x5588 , 0x09},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_brightness_neg2_regs[] = {
	 //brightness -2
	 {0x3212 , 0x03},
	 {0x5587 , 0x20},
	 {0x5588 , 0x09},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_brightness_neg1_regs[] = {
	 //brightness -1
	 {0x3212 , 0x03},
	 {0x5587 , 0x10},
	 {0x5588 , 0x09},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_brightness_zero_regs[] = {
	 //brightness 0
	 {0x3212 , 0x03},
	 {0x5587 , 0x00},
	 {0x5588 , 0x01},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_brightness_pos1_regs[] = {
	 //brightness +1
	 {0x3212 , 0x03},
	 {0x5587 , 0x10},
	 {0x5588 , 0x01},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_brightness_pos2_regs[] = {
	 //brightness +2
	 {0x3212 , 0x03},
	 {0x5587 , 0x20},
	 {0x5588 , 0x01},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_brightness_pos3_regs[] = {
	 //brightness +3
	 {0x3212 , 0x03},
	 {0x5587 , 0x30},
	 {0x5588 , 0x01},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_brightness_pos4_regs[] = {
	 //brightness +4
	 {0x3212 , 0x03},
	 {0x5587 , 0x40},
	 {0x5588 , 0x01},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct cfg_array sensor_brightness[] = {
	 {
	  .regs = sensor_brightness_neg4_regs,
	  .size = ARRAY_SIZE(sensor_brightness_neg4_regs),
	  },
	 {
	  .regs = sensor_brightness_neg3_regs,
	  .size = ARRAY_SIZE(sensor_brightness_neg3_regs),
	  },
	 {
	  .regs = sensor_brightness_neg2_regs,
	  .size = ARRAY_SIZE(sensor_brightness_neg2_regs),
	  },
	 {
	  .regs = sensor_brightness_neg1_regs,
	  .size = ARRAY_SIZE(sensor_brightness_neg1_regs),
	  },
	 {
	  .regs = sensor_brightness_zero_regs,
	  .size = ARRAY_SIZE(sensor_brightness_zero_regs),
	  },
	 {
	  .regs = sensor_brightness_pos1_regs,
	  .size = ARRAY_SIZE(sensor_brightness_pos1_regs),
	  },
	 {
	  .regs = sensor_brightness_pos2_regs,
	  .size = ARRAY_SIZE(sensor_brightness_pos2_regs),
	  },
	 {
	  .regs = sensor_brightness_pos3_regs,
	  .size = ARRAY_SIZE(sensor_brightness_pos3_regs),
	  },
	 {
	  .regs = sensor_brightness_pos4_regs,
	  .size = ARRAY_SIZE(sensor_brightness_pos4_regs),
	  },
 };

 //4.10.2 contrast
 static struct regval_list sensor_contrast_neg3_regs[] = {
	 //contrast -3
	 {0x3212 , 0x03},
	 {0x5586 , 0x14},
	 {0x5585 , 0x14},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_contrast_neg2_regs[] = {
	 //contrast -2
	 {0x3212 , 0x03},
	 {0x5586 , 0x18},
	 {0x5585 , 0x18},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_contrast_neg1_regs[] = {
	 //contrast -1
	 {0x3212 , 0x03},
	 {0x5586 , 0x1c},
	 {0x5585 , 0x1c},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_contrast_zero_regs[] = {
	 //contrast +0
	 {0x3212 , 0x03},
	 {0x5586 , 0x20},
	 {0x5585 , 0x00},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_contrast_pos1_regs[] = {
	 //contrast +1
	 {0x3212 , 0x03},
	 {0x5586 , 0x24},
	 {0x5585 , 0x10},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_contrast_pos2_regs[] = {
	 //contrast +2
	 {0x3212 , 0x03},
	 {0x5586 , 0x28},
	 {0x5585 , 0x18},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_contrast_pos3_regs[] = {
	 //contrast +3
	 {0x3212 , 0x03},
	 {0x5586 , 0x2c},
	 {0x5585 , 0x1c},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 
 static struct cfg_array sensor_contrast[] = {
	 {
	  .regs = sensor_contrast_neg3_regs,
	  .size = ARRAY_SIZE(sensor_contrast_neg3_regs),
	  },
	 {
	  .regs = sensor_contrast_neg2_regs,
	  .size = ARRAY_SIZE(sensor_contrast_neg2_regs),
	  },
	 {
	  .regs = sensor_contrast_neg1_regs,
	  .size = ARRAY_SIZE(sensor_contrast_neg1_regs),
	  },
	 {
	  .regs = sensor_contrast_zero_regs,
	  .size = ARRAY_SIZE(sensor_contrast_zero_regs),
	  },
	 {
	  .regs = sensor_contrast_pos1_regs,
	  .size = ARRAY_SIZE(sensor_contrast_pos1_regs),
	  },
	 {
	  .regs = sensor_contrast_pos2_regs,
	  .size = ARRAY_SIZE(sensor_contrast_pos2_regs),
	  },
	 {
	  .regs = sensor_contrast_pos3_regs,
	  .size = ARRAY_SIZE(sensor_contrast_pos3_regs),
	  },
 };

 //4.10.3 saturation
 static struct regval_list sensor_saturation_neg3_regs[] = {
	 //saturation -3
	 {0x3212 , 0x03},
	 {0x5381 , 0x1c},
	 {0x5382 , 0x5a},
	 {0x5383 , 0x06},
	 {0x5384 , 0x0c},
	 {0x5385 , 0x30},
	 {0x5386 , 0x3d},
	 {0x5387 , 0x3e},
	 {0x5388 , 0x3d},
	 {0x5389 , 0x01},
	 {0x538b , 0x98},
	 {0x538a , 0x01},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_saturation_neg2_regs[] = {
	 //saturation -2
	 {0x3212 , 0x03},
	 {0x5381 , 0x1c},
	 {0x5382 , 0x5a},
	 {0x5383 , 0x06},
	 {0x5384 , 0x10},
	 {0x5385 , 0x3d},
	 {0x5386 , 0x4d},
	 {0x5387 , 0x4e},
	 {0x5388 , 0x4d},
	 {0x5389 , 0x01},
	 {0x538b , 0x98},
	 {0x538a , 0x01},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_saturation_neg1_regs[] = {
	 //saturation -1
	 {0x3212 , 0x03},
	 {0x5381 , 0x1c},
	 {0x5382 , 0x5a},
	 {0x5383 , 0x06},
	 {0x5384 , 0x15},
	 {0x5385 , 0x52},
	 {0x5386 , 0x66},
	 {0x5387 , 0x68},
	 {0x5388 , 0x66},
	 {0x5389 , 0x02},
	 {0x538b , 0x98},
	 {0x538a , 0x01},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_saturation_zero_regs[] = {
	 //saturation +0
	 {0x3212 , 0x03},
	 {0x5381 , 0x1c},
	 {0x5382 , 0x5a},
	 {0x5383 , 0x06},
	 {0x5384 , 0x1a},
	 {0x5385 , 0x66},
	 {0x5386 , 0x80},
	 {0x5387 , 0x82},
	 {0x5388 , 0x80},
	 {0x5389 , 0x02},
	 {0x538b , 0x98},
	 {0x538a , 0x01},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_saturation_pos1_regs[] = {
	 //saturation +1
	 {0x3212 , 0x03},
	 {0x5381 , 0x1c},
	 {0x5382 , 0x5a},
	 {0x5383 , 0x06},
	 {0x5384 , 0x1f},
	 {0x5385 , 0x7a},
	 {0x5386 , 0x9a},
	 {0x5387 , 0x9c},
	 {0x5388 , 0x9a},
	 {0x5389 , 0x02},
	 {0x538b , 0x98},
	 {0x538a , 0x01},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_saturation_pos2_regs[] = {
	 //saturation +2
	 {0x3212 , 0x03},
	 {0x5381 , 0x1c},
	 {0x5382 , 0x5a},
	 {0x5383 , 0x06},
	 {0x5384 , 0x24},
	 {0x5385 , 0x8f},
	 {0x5386 , 0xb3},
	 {0x5387 , 0xb6},
	 {0x5388 , 0xb3},
	 {0x5389 , 0x03},
	 {0x538b , 0x98},
	 {0x538a , 0x01},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 static struct regval_list sensor_saturation_pos3_regs[] = {
	 //saturation +3
	 {0x3212 , 0x03},
	 {0x5381 , 0x1c},
	 {0x5382 , 0x5a},
	 {0x5383 , 0x06},
	 {0x5384 , 0x2b},
	 {0x5385 , 0xab},
	 {0x5386 , 0xd6},
	 {0x5387 , 0xda},
	 {0x5388 , 0xd6},
	 {0x5389 , 0x04},
	 {0x538b , 0x98},
	 {0x538a , 0x01},
	 {0x3212 , 0x13},
	 {0x3212 , 0xa3},
 };
 
 
 static struct cfg_array sensor_saturation[] = {
	 {
	  .regs = sensor_saturation_neg3_regs,
	  .size = ARRAY_SIZE(sensor_saturation_neg3_regs),
	  },
	 {
	  .regs = sensor_saturation_neg2_regs,
	  .size = ARRAY_SIZE(sensor_saturation_neg2_regs),
	  },
	 {
	  .regs = sensor_saturation_neg1_regs,
	  .size = ARRAY_SIZE(sensor_saturation_neg1_regs),
	  },
	 {
	  .regs = sensor_saturation_zero_regs,
	  .size = ARRAY_SIZE(sensor_saturation_zero_regs),
	  },
	 {
	  .regs = sensor_saturation_pos1_regs,
	  .size = ARRAY_SIZE(sensor_saturation_pos1_regs),
	  },
	 {
	  .regs = sensor_saturation_pos2_regs,
	  .size = ARRAY_SIZE(sensor_saturation_pos2_regs),
	  },
	 {
	  .regs = sensor_saturation_pos3_regs,
	  .size = ARRAY_SIZE(sensor_saturation_pos3_regs),
	  },
 };

 //4.10.4 EV
 static struct regval_list sensor_ev_neg3_regs[] = {
	 //EV -3
	 {0x3a0f, 0x10},
	 {0x3a10, 0x08},
	 {0x3a1b, 0x10},
	 {0x3a1e, 0x08},
	 {0x3a11, 0x20},
	 {0x3a1f, 0x10},
 
 };
 
 static struct regval_list sensor_ev_neg2_regs[] = {
	 //EV -2
	 {0x3a0f, 0x20},
	 {0x3a10, 0x18},
	 {0x3a11, 0x41},
	 {0x3a1b, 0x20},
	 {0x3a1e, 0x18},
	 {0x3a1f, 0x10},
 
 };
 
 static struct regval_list sensor_ev_neg1_regs[] = {
	 //EV -1
	 {0x3a0f, 0x30},
	 {0x3a10, 0x28},
	 {0x3a11, 0x61},
	 {0x3a1b, 0x30},
	 {0x3a1e, 0x28},
	 {0x3a1f, 0x10},
 };
 
 static struct regval_list sensor_ev_zero_regs[] = {
	 //EV +0
	 {0x3a0f, 0x38},
	 {0x3a10, 0x30},
	 {0x3a11, 0x61},
	 {0x3a1b, 0x38},
	 {0x3a1e, 0x30},
	 {0x3a1f, 0x10},
 };
 
 static struct regval_list sensor_ev_pos1_regs[] = {
	 //EV +1
	 {0x3a0f, 0x40},
	 {0x3a10, 0x38},
	 {0x3a11, 0x71},
	 {0x3a1b, 0x40},
	 {0x3a1e, 0x38},
	 {0x3a1f, 0x10},
 };
 
 static struct regval_list sensor_ev_pos2_regs[] = {
	 //EV +2
	 {0x3a0f, 0x50},
	 {0x3a10, 0x48},
	 {0x3a11, 0x90},
	 {0x3a1b, 0x50},
	 {0x3a1e, 0x48},
	 {0x3a1f, 0x20},
 };
 
 static struct regval_list sensor_ev_pos3_regs[] = {
	 //EV +3
	 {0x3a0f, 0x60},
	 {0x3a10, 0x58},
	 {0x3a11, 0xa0},
	 {0x3a1b, 0x60},
	 {0x3a1e, 0x58},
	 {0x3a1f, 0x20},
 };
 
 
 
 static struct cfg_array sensor_ev[] = {
	 {
	  .regs = sensor_ev_neg3_regs,
	  .size = ARRAY_SIZE(sensor_ev_neg3_regs),
	  },
	 {
	  .regs = sensor_ev_neg2_regs,
	  .size = ARRAY_SIZE(sensor_ev_neg2_regs),
	  },
	 {
	  .regs = sensor_ev_neg1_regs,
	  .size = ARRAY_SIZE(sensor_ev_neg1_regs),
	  },
	 {
	  .regs = sensor_ev_zero_regs,
	  .size = ARRAY_SIZE(sensor_ev_zero_regs),
	  },
	 {
	  .regs = sensor_ev_pos1_regs,
	  .size = ARRAY_SIZE(sensor_ev_pos1_regs),
	  },
	 {
	  .regs = sensor_ev_pos2_regs,
	  .size = ARRAY_SIZE(sensor_ev_pos2_regs),
	  },
	 {
	  .regs = sensor_ev_pos3_regs,
	  .size = ARRAY_SIZE(sensor_ev_pos3_regs),
	  },
 };

static struct regval_list sensor_fmt_yuv422_yuyv[] = {
	{0x4300, 0x30},
};

static struct regval_list sensor_fmt_yuv422_yvyu[] = {
	{0x4300, 0x31},
};

static struct regval_list sensor_fmt_yuv422_vyuy[] = {
	{0x4300, 0x33},
};

static struct regval_list sensor_fmt_yuv422_uyvy[] = {
	{0x4300, 0x32},
};


static struct regval_list sensor_fmt_raw[] = {
};

static struct regval_list ae_average_tbl[] = {
};

static struct regval_list ae_centerweight_tbl[] = {
};


static data_type current_lum = 0xff;
static data_type sensor_get_lum(struct v4l2_subdev *sd)
{
	return 0;
}

/* stuff about exposure when capturing image and video*/
static int sensor_s_denoise_value(struct v4l2_subdev *sd, data_type value);
data_type ogain, oexposurelow, oexposuremid, oexposurehigh;
unsigned int preview_exp_line, preview_fps;
unsigned long preview_pclk;

static unsigned int cal_cap_gain(data_type prv_gain, data_type lum)
{
	return 0;
}

static int sensor_set_capture_exposure(struct v4l2_subdev *sd)
{
	return 0;
}

static int sensor_get_pclk(struct v4l2_subdev *sd)
{
	return 0;
}


static int sensor_get_fps(struct v4l2_subdev *sd)
{
	return 0;
}


static int sensor_get_preview_exposure(struct v4l2_subdev *sd)
{
	data_type vts_low, vts_high, vts_extra_high, vts_extra_low;
	unsigned long vts, vts_extra;

	sensor_read(sd, 0x350b, &ogain);
	sensor_read(sd, 0x3502, &oexposurelow);
	sensor_read(sd, 0x3501, &oexposuremid);
	sensor_read(sd, 0x3500, &oexposurehigh);
	sensor_read(sd, 0x380e, &vts_high);
	sensor_read(sd, 0x380f, &vts_low);
	sensor_read(sd, 0x350c, &vts_extra_high);
	sensor_read(sd, 0x350d, &vts_extra_low);

	vts = vts_high * 256 + vts_low;
	vts_extra = vts_extra_high * 256 + vts_extra_low;
	preview_exp_line = vts + vts_extra;

	sensor_dbg("preview_exp_line = %d\n", preview_exp_line);

	return 0;
}

static int sensor_set_preview_exposure(struct v4l2_subdev *sd)
{
	data_type rdval;

	sensor_read(sd, 0x3503, &rdval);

	sensor_write(sd, 0x350b, ogain);
	sensor_write(sd, 0x3502, oexposurelow);
	sensor_write(sd, 0x3501, oexposuremid);
	sensor_write(sd, 0x3500, oexposurehigh);
	return 0;
}


#ifdef _FLASH_FUNC_
void check_to_flash(struct v4l2_subdev *sd)
{
	return 0;
}

#endif

/* stuff about auto focus */

static int sensor_download_af_fw(struct v4l2_subdev *sd)
{
	return 0;
}


static int sensor_g_single_af(struct v4l2_subdev *sd)
{
	return 0;
}


static int sensor_g_contin_af(struct v4l2_subdev *sd)
{
	return 0;
}


static int sensor_g_af_status(struct v4l2_subdev *sd)
{
	return 0;
}


static int sensor_g_3a_lock(struct v4l2_subdev *sd)
{
	return 0;
}


static int sensor_s_init_af(struct v4l2_subdev *sd)
{
	return 0;
}

static int sensor_s_single_af(struct v4l2_subdev *sd)
{
	return 0;
}

static int sensor_s_continueous_af(struct v4l2_subdev *sd, int value)
{
	return 0;
}

static int sensor_s_pause_af(struct v4l2_subdev *sd)
{
	return 0;
}

static int sensor_s_release_af(struct v4l2_subdev *sd)
{
	return 0;
}

#if 1
static int sensor_s_relaunch_af_zone(struct v4l2_subdev *sd)
{
	return 0;
}
#endif

static int sensor_s_af_zone(struct v4l2_subdev *sd,
			    struct v4l2_win_coordinate *win_c)
{
	return 0;
}


static int sensor_s_3a_lock(struct v4l2_subdev *sd, int value)
{
	return 0;
}


#if 1
static int sensor_s_sharpness_auto(struct v4l2_subdev *sd)
{
	return 0;
}

#endif

static int sensor_s_sharpness_value(struct v4l2_subdev *sd, data_type value)
{
	return 0;
}


#if 1
static int sensor_s_denoise_auto(struct v4l2_subdev *sd)
{
	return 0;
}

#endif

static int sensor_s_denoise_value(struct v4l2_subdev *sd, data_type value)
{
	return 0;
}


/* **********************begin of ******************************* */

static int sensor_g_hflip(struct v4l2_subdev *sd, __s32 *value)
{
	return 0;
}


static int sensor_s_hflip(struct v4l2_subdev *sd, int value)
{
	return 0;
}


static int sensor_g_vflip(struct v4l2_subdev *sd, __s32 *value)
{
	return 0;
}


static int sensor_s_vflip(struct v4l2_subdev *sd, int value)
{
	return 0;
}


static int sensor_g_autogain(struct v4l2_subdev *sd, __s32 *value)
{
	return 0;
}


static int sensor_s_autogain(struct v4l2_subdev *sd, int value)
{
	return 0;
}


static int sensor_g_autoexp(struct v4l2_subdev *sd, __s32 *value)
{
	return 0;
}


static int sensor_s_autoexp(struct v4l2_subdev *sd,
			    enum v4l2_exposure_auto_type value)
{
	return 0;
}


static int sensor_g_autowb(struct v4l2_subdev *sd, int *value)
{
	return 0;
}


static int sensor_s_autowb(struct v4l2_subdev *sd, int value)
{
	return 0;
}


int ov5640_sensor_vts;
static int sensor_s_exp_gain(struct v4l2_subdev *sd,
			     struct sensor_exp_gain *exp_gain)
{
	return 0;
}


static int sensor_g_hue(struct v4l2_subdev *sd, __s32 *value)
{
	return -EINVAL;
}

static int sensor_s_hue(struct v4l2_subdev *sd, int value)
{
	return -EINVAL;
}

static int sensor_g_gain(struct v4l2_subdev *sd, __s32 *value)
{
	return -EINVAL;
}

static int sensor_s_gain(struct v4l2_subdev *sd, int value)
{
	return -EINVAL;
}

static int sensor_g_band_filter(struct v4l2_subdev *sd, __s32 *value)
{
	struct sensor_info *info = to_state(sd);
	data_type rdval;

	sensor_read(sd, 0x3a00, &rdval);

	if ((rdval & (1 << 5)) == (1 << 5))
		info->band_filter = V4L2_CID_POWER_LINE_FREQUENCY_DISABLED;
	else {
		sensor_read(sd, 0x3c00, &rdval);
		if ((rdval & (1 << 2)) == (1 << 2))
			info->band_filter = V4L2_CID_POWER_LINE_FREQUENCY_50HZ;
		else
			info->band_filter = V4L2_CID_POWER_LINE_FREQUENCY_60HZ;
	}
	return 0;
}



static int sensor_s_band_filter(struct v4l2_subdev *sd,
				enum v4l2_power_line_frequency value)
{
	struct sensor_info *info = to_state(sd);
	data_type rdval;

	if (info->band_filter == value)
		return 0;

	switch (value) {
	case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
		sensor_read(sd, 0x3a00, &rdval);
		sensor_write(sd, 0x3a00, rdval & 0xdf);
		break;
	case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
		sensor_write(sd, 0x3c00, 0x04);
		sensor_write(sd, 0x3c01, 0x80);
		sensor_read(sd, 0x3a00, &rdval);
		sensor_write(sd, 0x3a00, rdval | 0x20);
		break;
	case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
		sensor_write(sd, 0x3c00, 0x00);
		sensor_write(sd, 0x3c01, 0x80);
		sensor_read(sd, 0x3a00, &rdval);
		sensor_write(sd, 0x3a00, rdval | 0x20);
		break;
	case V4L2_CID_POWER_LINE_FREQUENCY_AUTO:
		break;
	default:
		break;
	}

	info->band_filter = value;
	return 0;
}



/* ********************************end of ******************************* */

static int sensor_g_brightness(struct v4l2_subdev *sd, __s32 *value)
{
	struct sensor_info *info = to_state(sd);

	*value = info->brightness;
	return 0;
}

static int sensor_s_brightness(struct v4l2_subdev *sd, int value)
{
	struct sensor_info *info = to_state(sd);

	if (info->brightness == value)
		return 0;

	if (value < 0 || value >= sizeof(sensor_brightness))
		return -ERANGE;

	sensor_write_array(sd, sensor_brightness[value].regs,
			sensor_brightness[value].size);

	info->brightness = value;
	return 0;
}



static int sensor_g_contrast(struct v4l2_subdev *sd, __s32 *value)
{
	struct sensor_info *info = to_state(sd);

	*value = info->contrast;
	return 0;
}

static int sensor_s_contrast(struct v4l2_subdev *sd, int value)
{
	struct sensor_info *info = to_state(sd);

	if (info->contrast == value)
		return 0;

	if (value < 0 || value >= sizeof(sensor_contrast))
		return -ERANGE;

	sensor_write_array(sd, sensor_contrast[value].regs,
			sensor_contrast[value].size);

	info->contrast = value;
	return 0;
}

static int sensor_g_saturation(struct v4l2_subdev *sd, __s32 *value)
{
	struct sensor_info *info = to_state(sd);

	*value = info->saturation;
	return 0;
}

static int sensor_s_saturation(struct v4l2_subdev *sd, int value)
{
	struct sensor_info *info = to_state(sd);

	if (info->saturation == value)
		return 0;

	if (value < 0 || value >= sizeof(sensor_saturation))
		return -ERANGE;

	sensor_write_array(sd, sensor_saturation[value].regs,
			sensor_saturation[value].size);

	info->saturation = value;
	return 0;
}

static int sensor_g_exp_bias(struct v4l2_subdev *sd, __s32 *value)
{
	struct sensor_info *info = to_state(sd);

	*value = info->exp_bias;
	return 0;
}

static int sensor_s_exp_bias(struct v4l2_subdev *sd, int value)
{
	struct sensor_info *info = to_state(sd);

	if (info->exp_bias == value)
		return 0;

	if (value < 0 || value > 8)
		return -ERANGE;

	sensor_write(sd, 0x3503, 0x07);
	sensor_get_preview_exposure(sd);
	sensor_write(sd, 0x3503, 0x00);

	sensor_write_array(sd, sensor_ev[value].regs,
			sensor_ev[value].size);

	info->exp_bias = value;
	return 0;
}


static int sensor_g_wb(struct v4l2_subdev *sd, int *value)
{
	struct sensor_info *info = to_state(sd);
	enum v4l2_auto_n_preset_white_balance *wb_type =
	    (enum v4l2_auto_n_preset_white_balance *)value;

	*wb_type = info->wb;

	return 0;
}

static int sensor_s_wb(struct v4l2_subdev *sd,
		       enum v4l2_auto_n_preset_white_balance value)
{
	struct sensor_info *info = to_state(sd);

	if (info->capture_mode == V4L2_MODE_IMAGE)
		return 0;

	if (info->wb == value)
		return 0;
	int sensor_wb_index = 0;
	switch(value){
		case V4L2_WHITE_BALANCE_AUTO:
			sensor_wb_index = 0;
			break;
		case V4L2_WHITE_BALANCE_DAYLIGHT:
			sensor_wb_index = 1;
			break;
		case V4L2_WHITE_BALANCE_FLUORESCENT_H:
			sensor_wb_index = 2;
			break;
		case V4L2_WHITE_BALANCE_CLOUDY:
			sensor_wb_index = 3;
			break;
		case V4L2_WHITE_BALANCE_FLUORESCENT:
			sensor_wb_index = 4;
			break;
		default :
			break;
	}
	sensor_write_array(sd, sensor_wb[sensor_wb_index].regs,
			sensor_wb[sensor_wb_index].size);

	if (value == V4L2_WHITE_BALANCE_AUTO)
		info->autowb = 1;
	else
		info->autowb = 0;

	info->wb = value;
	return 0;
}

static int sensor_g_colorfx(struct v4l2_subdev *sd, __s32 *value)
{
	struct sensor_info *info = to_state(sd);
	enum v4l2_colorfx *clrfx_type = (enum v4l2_colorfx *)value;

	*clrfx_type = info->clrfx;
	return 0;
}

static int sensor_s_colorfx(struct v4l2_subdev *sd, enum v4l2_colorfx value)
{
	struct sensor_info *info = to_state(sd);

	if (info->clrfx == value)
		return 0;
	int colorfx_index = 0;
	switch(value){
		case V4L2_COLORFX_NONE:
			colorfx_index = 0;
			break;
		case V4L2_COLORFX_SKY_BLUE:
			colorfx_index = 1;
			break;
		case V4L2_COLORFX_EMBOSS:
			colorfx_index = 2;
			break;
		case V4L2_COLORFX_BW:
			colorfx_index = 3;
			break;
		case V4L2_COLORFX_SEPIA:
			colorfx_index = 4;
			break;
		case V4L2_COLORFX_NEGATIVE:
			colorfx_index = 5;
			break;
		case V4L2_COLORFX_GRASS_GREEN:
			colorfx_index = 6;
			break;
		case V4L2_COLORFX_SKETCH:
			colorfx_index = 7;
			break;
		case V4L2_COLORFX_SOLARIZATION:
			colorfx_index = 8;
			break;
		default:
			break;
	}
	sensor_write_array(sd, sensor_colorfx[colorfx_index].regs,
			sensor_colorfx[colorfx_index].size);

	info->clrfx = value;
	return 0;
}

static int sensor_g_flash_mode(struct v4l2_subdev *sd, __s32 *value)
{
	return 0;
}


static int sensor_s_flash_mode(struct v4l2_subdev *sd,
		       enum v4l2_flash_led_mode value)
{
	return 0;
}


/*
 * Stuff that knows about the sensor.
 */

static int sensor_power(struct v4l2_subdev *sd, int on)
{
	int ret = 0;

	switch (on) {
	case STBY_ON:
		sensor_dbg("STBY_OFF not implement!\n");
		break;
	case STBY_OFF:
		sensor_dbg("STBY_OFF not implement!\n");
		break;
	case PWR_ON:
		sensor_print("PWR_ON!\n");
		cci_lock(sd);
		vin_set_pmu_channel(sd, CAMERAVDD, ON);
		vin_gpio_set_status(sd, PWDN, 1);
		vin_gpio_set_status(sd, RESET, 1);
		vin_gpio_write(sd, PWDN, CSI_GPIO_HIGH);
		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
		usleep_range(1000, 1200);
		vin_set_mclk_freq(sd, MCLK / MCLK_DIV);
		vin_set_mclk(sd, ON);
		usleep_range(10000, 12000);
		vin_gpio_write(sd, POWER_EN, CSI_GPIO_HIGH);
		vin_set_pmu_channel(sd, IOVDD, ON);
		vin_set_pmu_channel(sd, AVDD, ON);
		vin_set_pmu_channel(sd, DVDD, ON);
		vin_set_pmu_channel(sd, AFVDD, ON);
		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
		usleep_range(10000, 12000);
		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
		usleep_range(30000, 31000);
		cci_unlock(sd);
		break;
	case PWR_OFF:
		sensor_print("PWR_OFF!\n");
		cci_lock(sd);
		vin_set_mclk(sd, OFF);
		vin_gpio_write(sd, POWER_EN, CSI_GPIO_LOW);
		vin_set_pmu_channel(sd, AFVDD, OFF);
		vin_set_pmu_channel(sd, DVDD, OFF);
		vin_set_pmu_channel(sd, AVDD, OFF);
		vin_set_pmu_channel(sd, IOVDD, OFF);
		vin_set_pmu_channel(sd, CAMERAVDD, OFF);
		usleep_range(10000, 12000);
		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
		vin_gpio_set_status(sd, RESET, 0);
		vin_gpio_set_status(sd, PWDN, 0);
		cci_unlock(sd);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int sensor_reset(struct v4l2_subdev *sd, u32 val)
{
	switch (val) {
	case 0:
		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
		usleep_range(10000, 12000);
		break;
	case 1:
		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
		usleep_range(10000, 12000);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int sensor_detect(struct v4l2_subdev *sd)
{
	unsigned int SENSOR_ID = 0;
	data_type val;
	int cnt = 0;

	sensor_read(sd, 0x300a, &val);
	SENSOR_ID |= (val << 8);
	sensor_read(sd, 0x300b, &val);
	SENSOR_ID |= (val);
	sensor_print("V4L2_IDENT_SENSOR = %x\n", SENSOR_ID);

	while ((SENSOR_ID != V4L2_IDENT_SENSOR) && (cnt < 5)) {
		sensor_read(sd, 0x300a, &val);
		SENSOR_ID |= (val << 8);
		sensor_read(sd, 0x300b, &val);
		SENSOR_ID |= (val);
		sensor_print("retry = %d, V4L2_IDENT_SENSOR = %x\n", cnt, SENSOR_ID);
		cnt++;
	}

	if (SENSOR_ID != V4L2_IDENT_SENSOR)
		return -ENODEV;

	return 0;

}

static int sensor_init(struct v4l2_subdev *sd, u32 val)
{
	int ret;
	struct sensor_info *info = to_state(sd);

	sensor_dbg("sensor_init 0x%x\n", val);

	/*Make sure it is a target sensor */
	ret = sensor_detect(sd);
	if (ret) {
		sensor_err("chip found is not an target chip.\n");
		return ret;
	}

	ogain = 0x28;
	oexposurelow = 0x00;
	oexposuremid = 0x3d;
	oexposurehigh = 0x00;
	info->focus_status = 0;
	info->low_speed = 0;
	info->width = 0;
	info->height = 0;
	info->brightness = 0;
	info->contrast = 0;
	info->saturation = 0;
	info->hue = 0;
	info->hflip = 0;
	info->vflip = 0;
	info->gain = 0;
	info->autogain = 1;
	info->exp_bias = 0;
	info->autoexp = 1;
	info->autowb = 1;
	info->wb = V4L2_WHITE_BALANCE_AUTO;
	info->clrfx = V4L2_COLORFX_NONE;
	info->band_filter = V4L2_CID_POWER_LINE_FREQUENCY_50HZ;


	info->tpf.numerator = 1;
	info->tpf.denominator = 30;	/* 30fps */

	ret =
		sensor_write_array(sd, sensor_init_regs,
				   ARRAY_SIZE(sensor_init_regs));
	if (ret < 0) {
		sensor_err("write sensor_default_regs error\n");
		return ret;
	}

	sensor_s_band_filter(sd, V4L2_CID_POWER_LINE_FREQUENCY_50HZ);

	info->preview_first_flag = 1;
	night_mode = 0;
	Nfrms = MAX_FRM_CAP;

	if (1 == AE_CW)
		sensor_write_array(sd, ae_centerweight_tbl,
				   ARRAY_SIZE(ae_centerweight_tbl));
	else
		sensor_write_array(sd, ae_average_tbl,
				   ARRAY_SIZE(ae_average_tbl));

	return 0;
}

static int sensor_g_exif(struct v4l2_subdev *sd,
			 struct sensor_exif_attribute *exif)
{
	int ret = 0;

	exif->fnumber = 220;
	exif->focal_length = 180;
	exif->brightness = 125;
	exif->flash_fire = 0;
	exif->iso_speed = 200;
	exif->exposure_time_num = 1;
	exif->exposure_time_den = 15;
	return ret;
}
static void sensor_s_af_win(struct v4l2_subdev *sd,
			    struct v4l2_win_setting *af_win)
{
	sensor_s_af_zone(sd, &af_win->coor);
}
static void sensor_s_ae_win(struct v4l2_subdev *sd,
			    struct v4l2_win_setting *ae_win)
{

}

static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
{
	int ret = 0;

	switch (cmd) {
	case GET_SENSOR_EXIF:
		sensor_g_exif(sd, (struct sensor_exif_attribute *)arg);
		break;
	case SET_AUTO_FOCUS_WIN:
		sensor_s_af_win(sd, (struct v4l2_win_setting *)arg);
		break;
	case SET_AUTO_EXPOSURE_WIN:
		sensor_s_ae_win(sd, (struct v4l2_win_setting *)arg);
		break;
	case VIDIOC_VIN_SENSOR_EXP_GAIN:
		sensor_s_exp_gain(sd, (struct sensor_exp_gain *)arg);
		break;
	case VIDIOC_VIN_SENSOR_CFG_REQ:
		sensor_cfg_req(sd, (struct sensor_config *)arg);
		break;

	default:
		return -EINVAL;
	}
	return ret;
}

/*
 * Store information about the video data format.
 */
static struct sensor_format_struct sensor_formats[] = {
	{
		.desc = "YUYV 4:2:2",
		.mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
		.regs = sensor_fmt_yuv422_yuyv,
		.regs_size = ARRAY_SIZE(sensor_fmt_yuv422_yuyv),
		.bpp = 2,
	}, {
		.desc = "YVYU 4:2:2",
		.mbus_code = MEDIA_BUS_FMT_YVYU8_2X8,
		.regs = sensor_fmt_yuv422_yvyu,
		.regs_size = ARRAY_SIZE(sensor_fmt_yuv422_yvyu),
		.bpp = 2,
	}, {
		.desc = "UYVY 4:2:2",
		.mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
		.regs = sensor_fmt_yuv422_uyvy,
		.regs_size = ARRAY_SIZE(sensor_fmt_yuv422_uyvy),
		.bpp = 2,
	}, {
		.desc = "VYUY 4:2:2",
		.mbus_code = MEDIA_BUS_FMT_VYUY8_2X8,
		.regs = sensor_fmt_yuv422_vyuy,
		.regs_size = ARRAY_SIZE(sensor_fmt_yuv422_vyuy),
		.bpp = 2,
	},
};

#define N_FMTS ARRAY_SIZE(sensor_formats)

/*
 * Then there is the issue of window sizes.  Try to capture the info here.
 */

static struct sensor_win_size sensor_win_sizes[] = {
	/* qsxga: 2592*1936 */
	{
	.width = QSXGA_WIDTH,
	.height = QSXGA_HEIGHT,
	.hoffset = 0,
	.voffset = 0,
	.regs = sensor_5M_15fps_regs,
	.regs_size = ARRAY_SIZE(sensor_5M_15fps_regs),
	.set_size = NULL,
	},
	/* 720p */
	{
	.width = HD720_WIDTH,
	.height = HD720_HEIGHT,
	.hoffset = 0,
	.voffset = 0,
	.hts = 1750,
	.vts = 800,
	.pclk = 42 * 1000 * 1000,
	.fps_fixed = 1,
	.bin_factor = 1,
	.intg_min = 1,
	.intg_max = 800 << 4,
	.gain_min = 1 << 4,
	.gain_max = 10 << 4,
	.regs = sensor_720p_60fps_regs,
	.regs_size = ARRAY_SIZE(sensor_720p_60fps_regs),
	.set_size = NULL,
	},
	/* VGA */
	{
	.width = VGA_WIDTH,
	.height = VGA_HEIGHT,
	.hoffset = 0,
	.voffset = 0,
	.hts = 640,
	.vts = 480,
	.pclk = 9216 * 1000,
	.fps_fixed = 1,
	.bin_factor = 1,
	.intg_min = 1,
	.intg_max = 480 << 4,
	.gain_min = 1 << 4,
	.gain_max = 10 << 4,
	.regs = sensor_vga_30fps_regs,
	.regs_size = ARRAY_SIZE(sensor_vga_30fps_regs),
	.set_size = NULL,
	},
};

#define N_WIN_SIZES (ARRAY_SIZE(sensor_win_sizes))

static int sensor_g_mbus_config(struct v4l2_subdev *sd,unsigned int pad,
				struct v4l2_mbus_config *cfg)
{
	if(cfg == NULL) {
		return -1;
	}
	cfg->type = V4L2_MBUS_CSI2_DPHY;
	cfg->flags = 0 | V4L2_MBUS_CSI2_2_LANE | V4L2_MBUS_CSI2_CHANNEL_0;

	return 0;
}


/*
 * Code for dealing with controls.
 * fill with different sensor module
 * different sensor module has different settings here
 * if not support the follow function ,retrun -EINVAL
 */

/* *****************************begin of ******************************* */

static int sensor_g_ctrl(struct v4l2_ctrl *ctrl)
{
	struct sensor_info *info =
			container_of(ctrl->handler, struct sensor_info, handler);
	struct v4l2_subdev *sd = &info->sd;

	switch (ctrl->id) {
	case V4L2_CID_BRIGHTNESS:
		//implement brightness
		return sensor_g_brightness(sd, &ctrl->val);
	case V4L2_CID_CONTRAST:
		//implement contrast
		return sensor_g_contrast(sd, &ctrl->val);
	case V4L2_CID_SATURATION:
		return sensor_g_saturation(sd, &ctrl->val);
	case V4L2_CID_HUE:
		return sensor_g_hue(sd, &ctrl->val);
	case V4L2_CID_VFLIP:
		return sensor_g_vflip(sd, &ctrl->val);
	case V4L2_CID_HFLIP:
		return sensor_g_hflip(sd, &ctrl->val);
	case V4L2_CID_GAIN:
		return sensor_g_gain(sd, &ctrl->val);
	case V4L2_CID_AUTOGAIN:
		return sensor_g_autogain(sd, &ctrl->val);
	case V4L2_CID_EXPOSURE:
	case V4L2_CID_AUTO_EXPOSURE_BIAS:
		return sensor_g_exp_bias(sd, &ctrl->val);
	case V4L2_CID_EXPOSURE_AUTO:
		return sensor_g_autoexp(sd, &ctrl->val);
	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
		// implement wb
		return sensor_g_wb(sd, &ctrl->val);
	case V4L2_CID_AUTO_WHITE_BALANCE:
		return sensor_g_autowb(sd, &ctrl->val);
	case V4L2_CID_COLORFX:
		// implement colorfx
		return sensor_g_colorfx(sd, &ctrl->val);
	case V4L2_CID_FLASH_LED_MODE:
		return sensor_g_flash_mode(sd, &ctrl->val);
	case V4L2_CID_POWER_LINE_FREQUENCY:
		return sensor_g_band_filter(sd, &ctrl->val);
	case V4L2_CID_3A_LOCK:
		return sensor_g_3a_lock(sd);
	case V4L2_CID_AUTO_FOCUS_STATUS:
		return sensor_g_af_status(sd);

	}
	return -EINVAL;
}

static int sensor_s_ctrl(struct v4l2_ctrl *ctrl)
{
	struct sensor_info *info =
			container_of(ctrl->handler, struct sensor_info, handler);
	struct v4l2_subdev *sd = &info->sd;

	switch (ctrl->id) {
	case V4L2_CID_BRIGHTNESS:
		//implement brightness
		return sensor_s_brightness(sd, ctrl->val);
	case V4L2_CID_CONTRAST:
		//implement contrast
		return sensor_s_contrast(sd, ctrl->val);
	case V4L2_CID_SATURATION:
		return sensor_s_saturation(sd, ctrl->val);
	case V4L2_CID_HUE:
		return sensor_s_hue(sd, ctrl->val);
	case V4L2_CID_VFLIP:
		return sensor_s_vflip(sd, ctrl->val);
	case V4L2_CID_HFLIP:
		return sensor_s_hflip(sd, ctrl->val);
	case V4L2_CID_GAIN:
		return sensor_s_gain(sd, ctrl->val);
	case V4L2_CID_AUTOGAIN:
		return sensor_s_autogain(sd, ctrl->val);
	case V4L2_CID_EXPOSURE:
	case V4L2_CID_AUTO_EXPOSURE_BIAS:
		// implement sensor_s_exp_bias
		return sensor_s_exp_bias(sd, ctrl->val);
	case V4L2_CID_EXPOSURE_AUTO:
		return sensor_s_autoexp(sd,
				(enum v4l2_exposure_auto_type)ctrl->val);
	case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
		// implement wb
		return sensor_s_wb(sd,
			(enum v4l2_auto_n_preset_white_balance)ctrl->val);
	case V4L2_CID_AUTO_WHITE_BALANCE:
		return sensor_s_autowb(sd, ctrl->val);
	case V4L2_CID_COLORFX:
		// implement colorfx
		return sensor_s_colorfx(sd, (enum v4l2_colorfx)ctrl->val);
	case V4L2_CID_FLASH_LED_MODE:
		return sensor_s_flash_mode(sd,
				(enum v4l2_flash_led_mode)ctrl->val);
	case V4L2_CID_POWER_LINE_FREQUENCY:
		return sensor_s_band_filter(sd,
			(enum v4l2_power_line_frequency)ctrl->val);
	case V4L2_CID_3A_LOCK:
		return sensor_s_3a_lock(sd, ctrl->val);
	case V4L2_CID_AUTO_FOCUS_INIT:
		return sensor_s_init_af(sd);
	case V4L2_CID_AUTO_FOCUS_RELEASE:
		return sensor_s_release_af(sd);
	case V4L2_CID_AUTO_FOCUS_START:
		return sensor_s_single_af(sd);
	case V4L2_CID_AUTO_FOCUS_STOP:
		return sensor_s_pause_af(sd);
	case V4L2_CID_FOCUS_AUTO:
		return sensor_s_continueous_af(sd, ctrl->val);
	default:
		return -EINVAL;
	}
	return -EINVAL;
}

static int sensor_reg_init(struct sensor_info *info)
{
	int ret;
	struct v4l2_subdev *sd = &info->sd;
	struct sensor_format_struct *sensor_fmt = info->fmt;
	struct sensor_win_size *wsize = info->current_wins;

#ifdef _FLASH_FUNC_
	struct modules_config *modules = sd_to_modules(sd);
#endif
	sensor_dbg("sensor_reg_init\n");

	/*
	ret =
	    sensor_write_array(sd, sensor_default_regs,
			       ARRAY_SIZE(sensor_default_regs));
	if (ret < 0) {
		sensor_err("write sensor_default_regs error\n");
		return ret;
	}

	sensor_s_band_filter(sd, V4L2_CID_POWER_LINE_FREQUENCY_50HZ);

	info->preview_first_flag = 1;
	night_mode = 0;
	Nfrms = MAX_FRM_CAP;

	if (1 == AE_CW)
		sensor_write_array(sd, ae_centerweight_tbl,
				   ARRAY_SIZE(ae_centerweight_tbl));
	else
		sensor_write_array(sd, ae_average_tbl,
				   ARRAY_SIZE(ae_average_tbl));
	*/

	sensor_write_array(sd, sensor_oe_disable_regs,
			   ARRAY_SIZE(sensor_oe_disable_regs));

	if (info->capture_mode == V4L2_MODE_VIDEO) {

#ifdef _FLASH_FUNC_
		if (info->flash_mode != V4L2_FLASH_LED_MODE_NONE) {

			io_set_flash_ctrl(modules->modules.flash.sd,
					  SW_CTRL_FLASH_OFF);
		}
#endif
	} else if (info->capture_mode == V4L2_MODE_IMAGE) {

		ret = sensor_s_autoexp(sd, V4L2_EXPOSURE_MANUAL);
		if (ret < 0)
			sensor_err
			    ("sensor_s_autoexp off err when capturing image!\n");

		ret = sensor_s_autogain(sd, 0);
		if (ret < 0)
			sensor_err
			    ("sensor_s_autogain off err when capturing image!\n");

		if (wsize->width > SVGA_WIDTH) {
#ifdef _FLASH_FUNC_
			check_to_flash(sd);
#endif
			sensor_get_lum(sd);
			sensor_get_preview_exposure(sd);
			sensor_get_fps(sd);
		}
#ifdef _FLASH_FUNC_
		if (info->flash_mode != V4L2_FLASH_LED_MODE_NONE) {
			if (to_flash == 1) {
				sensor_dbg("open flash when capture\n");
				io_set_flash_ctrl(modules->modules.flash.sd,
						  SW_CTRL_FLASH_ON);
				sensor_get_lum(sd);
				sensor_get_preview_exposure(sd);
				sensor_get_fps(sd);
				msleep(50);
			}
		}
#endif
		ret = sensor_s_autowb(sd, 0);
		if (ret < 0)
			sensor_err("sensor_s_autowb off err when image mode\n");
	}

	sensor_write_array(sd, sensor_fmt->regs, sensor_fmt->regs_size);
	ret = sensor_write_array(sd, sensor_init_regs, ARRAY_SIZE(sensor_init_regs));
	if(ret < 0){
		sensor_err("ov5640_mipi write sensor_init_regs failed\n");
	} else {
		sensor_err("ov5640_mipi write sensor_init_regs success\n");
	}

	if (wsize->regs)
		sensor_write_array(sd, wsize->regs, wsize->regs_size);

	if (wsize->set_size)
		wsize->set_size(sd);

	sensor_s_hflip(sd, info->hflip);
	sensor_s_vflip(sd, info->vflip);

	if (info->capture_mode == V4L2_MODE_VIDEO ||
	    info->capture_mode == V4L2_MODE_PREVIEW) {
#ifdef AUTO_FPS
		if (info->capture_mode == V4L2_MODE_PREVIEW) {
			sensor_write_array(sd, sensor_auto_fps_mode,
					   ARRAY_SIZE(sensor_auto_fps_mode));
		} else {
			sensor_write_array(sd, sensor_fix_fps_mode,
					   ARRAY_SIZE(sensor_fix_fps_mode));
		}
#endif
		ret = sensor_set_preview_exposure(sd);
		if (ret < 0)
			sensor_err("sensor_set_preview_exposure err!\n");

		ret = sensor_s_autoexp(sd, V4L2_EXPOSURE_AUTO);
		if (ret < 0)
			sensor_err("sensor_s_autoexp on err  at video mode\n");

		ret = sensor_s_autogain(sd, 1);
		if (ret < 0)
			sensor_err("sensor_s_autogain on err at video mode\n");

		if (info->wb == V4L2_WHITE_BALANCE_AUTO) {
			ret = sensor_s_autowb(sd, 1);
			if (ret < 0)
				sensor_err
				    ("sensor_s_autowb on err at image mode\n");
		}

		if (info->capture_mode == V4L2_MODE_VIDEO) {

			if (wsize->width == 640) {
				sensor_s_sharpness_value(sd, 0x20);
				sensor_s_denoise_value(sd, 0x04);
			} else if (wsize->height == 960) {
				sensor_s_sharpness_value(sd, 0x08);
				sensor_s_denoise_value(sd, 0x08);
			} else if (wsize->height == 720) {
				sensor_s_sharpness_value(sd, 0x08);
				sensor_s_denoise_value(sd, 0x04);
			} else if (wsize->width == 1920) {
				sensor_s_sharpness_value(sd, 0x08);
				sensor_s_denoise_value(sd, 0x14);
			} else {
				sensor_s_sharpness_auto(sd);
				sensor_s_denoise_auto(sd);
			}
		} else if (info->capture_mode == V4L2_MODE_PREVIEW) {
			sensor_s_sharpness_value(sd, 0x20);
			sensor_s_denoise_value(sd, 0x10);
		}
		if (info->low_speed == 1) {
			if (info->preview_first_flag == 1) {
				info->preview_first_flag = 0;
				msleep(600);
			} else {
				msleep(200);
			}
		}
		if ((info->width != QSXGA_WIDTH)
		    && (info->preview_first_flag != 1)) {
			ret = sensor_s_relaunch_af_zone(sd);
			if (ret < 0)
				sensor_err("sensor_s_relaunch_af_zone err\n");
			ret = sensor_write(sd, 0x3022, 0x03);
			if (ret < 0)
				sensor_err("sensor_s_single_af err\n");
			if (info->auto_focus == 1)
				sensor_s_continueous_af(sd, 1);
			msleep(100);
		} else
			msleep(150);
	} else {
		if (wsize->width > SVGA_WIDTH) {
			ret = sensor_set_capture_exposure(sd);
			if (ret < 0)
				sensor_err("sensor_set_capture_exposure err\n");
		}

		sensor_s_sharpness_value(sd, SHARPNESS);

		if (info->low_speed == 1) {
			data_type rdval;

			sensor_read(sd, 0x3035, &rdval);
			sensor_write(sd, 0x3035,
				     (rdval & 0x0f) | ((rdval & 0xf0) * 2));

		}
		msleep(300);
	}
	info->width = wsize->width;
	info->height = wsize->height;
	ov5640_sensor_vts = wsize->vts;
	sensor_print("s_fmt set width = %d, height = %d\n", wsize->width,
		      wsize->height);
	sensor_write_array(sd, sensor_oe_enable_regs,
			   ARRAY_SIZE(sensor_oe_enable_regs));
	return 0;
}

static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
{
	struct sensor_info *info = to_state(sd);
	int ret = 0;

	sensor_print("%s on = %d, %d*%d %x\n", __func__, enable,
		  info->current_wins->width,
		  info->current_wins->height, info->fmt->mbus_code);

	if (!enable) {
		sensor_write(sd, 0x4202, 0x0f);
		return 0;
	}
	ret = sensor_reg_init(info);
	if (ret < 0) {
		sensor_err("sensor_reg_init error!\n");
		return ret;
	}
	sensor_err("[ov5640_mipi]sensor_reg_init success!\n");
	ret = sensor_write(sd, 0x4202, 0x00);
	if (ret < 0) {
		sensor_err("mipi stream on error!\n");
		return ret;
	}
	sensor_err("[ov5640_mipi]mipi stream on success!\n");
	{
		//sunxilong test read some reg
		/*	{0x3808 , 0x02},
			{0x3809 , 0x80},
			{0x380a , 0x01},
			{0x380b , 0xe0},*/
		data_type val0;
		sensor_read(sd, 0x3808, &val0);sensor_err("{0x%x , 0x%x}\n",0x3808,val0);
		sensor_read(sd, 0x3809, &val0);sensor_err("{0x%x , 0x%x}\n",0x3809,val0);
		sensor_read(sd, 0x380a, &val0);sensor_err("{0x%x , 0x%x}\n",0x380a,val0);
		sensor_read(sd, 0x380b, &val0);sensor_err("{0x%x , 0x%x}\n",0x380b,val0);
	}
	return ret;
}



/* ----------------------------------------------------------------------- */

static const struct v4l2_ctrl_ops sensor_ctrl_ops = {
	.g_volatile_ctrl = sensor_g_ctrl,
	.s_ctrl = sensor_s_ctrl,
};

static const s64 sensor_exp_bias_qmenu[] = {
	-3, -2, -1, 0, 1, 2, 3,
};

static const struct v4l2_ctrl_config sensor_custom_ctrls[] = {
	{
		.ops = &sensor_ctrl_ops,
		.id = V4L2_CID_AUTO_FOCUS_INIT,
		.name = "AutoFocus Initial",
		.type = V4L2_CTRL_TYPE_BUTTON,
		.min = 0,
		.max = 0,
		.step = 0,
		.def = 0,
	}, {
		.ops = &sensor_ctrl_ops,
		.id = V4L2_CID_AUTO_FOCUS_RELEASE,
		.name = "AutoFocus Release",
		.type = V4L2_CTRL_TYPE_BUTTON,
		.min = 0,
		.max = 0,
		.step = 0,
		.def = 0,
	},
};

static const struct v4l2_subdev_core_ops sensor_core_ops = {
	.reset = sensor_reset,
	.init = sensor_init,
	.s_power = sensor_power,
	.ioctl = sensor_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl32 = sensor_compat_ioctl32,
#endif
};

static const struct v4l2_subdev_video_ops sensor_video_ops = {
	.s_stream = sensor_s_stream,
};

static const struct v4l2_subdev_pad_ops sensor_pad_ops = {
	.enum_mbus_code = sensor_enum_mbus_code,
	.enum_frame_size = sensor_enum_frame_size,
	.get_fmt = sensor_get_fmt,
	.set_fmt = sensor_set_fmt,
	.get_mbus_config = sensor_g_mbus_config,
};

static const struct v4l2_subdev_ops sensor_ops = {
	.core = &sensor_core_ops,
	.video = &sensor_video_ops,
	.pad = &sensor_pad_ops,
};


/* ----------------------------------------------------------------------- */
static struct cci_driver cci_drv = {
	.name = SENSOR_NAME,
	.addr_width = CCI_BITS_16,
	.data_width = CCI_BITS_8,
};

static int sensor_init_controls(struct v4l2_subdev *sd, const struct v4l2_ctrl_ops *ops)
{
	struct sensor_info *info = to_state(sd);
	struct v4l2_ctrl_handler *handler = &info->handler;
	struct v4l2_ctrl *ctrl;
	int i;
	int ret = 0;

	v4l2_ctrl_handler_init(handler, 14 + ARRAY_SIZE(sensor_custom_ctrls));

	v4l2_ctrl_new_std(handler, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
	v4l2_ctrl_new_std(handler, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
	ctrl = v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE, 0,
			      65536 * 16, 1, 0);
	if (ctrl != NULL)
		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
	v4l2_ctrl_new_int_menu(handler, ops, V4L2_CID_AUTO_EXPOSURE_BIAS,
				ARRAY_SIZE(sensor_exp_bias_qmenu) - 1,
				ARRAY_SIZE(sensor_exp_bias_qmenu) / 2, sensor_exp_bias_qmenu);
	v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_EXPOSURE_AUTO,
				V4L2_EXPOSURE_APERTURE_PRIORITY, 0,
				V4L2_EXPOSURE_AUTO);
	v4l2_ctrl_new_std_menu(handler, ops,
				V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
				V4L2_WHITE_BALANCE_SHADE, 0,
				V4L2_WHITE_BALANCE_AUTO);
	v4l2_ctrl_new_std(handler, ops, V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
	v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_COLORFX,
				V4L2_COLORFX_SET_CBCR, 0, V4L2_COLORFX_NONE);
	v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_FLASH_LED_MODE,
				V4L2_FLASH_LED_MODE_RED_EYE, 0,
				V4L2_FLASH_LED_MODE_NONE);
	ctrl = v4l2_ctrl_new_std(handler, ops, V4L2_CID_3A_LOCK, 0, 7, 0, 0);
	if (ctrl != NULL)
		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
	for (i = 0; i < ARRAY_SIZE(sensor_custom_ctrls); i++)
		v4l2_ctrl_new_custom(handler, &sensor_custom_ctrls[i], NULL);
	v4l2_ctrl_new_std(handler, ops, V4L2_CID_AUTO_FOCUS_START, 0, 0, 0, 0);
	v4l2_ctrl_new_std(handler, ops, V4L2_CID_AUTO_FOCUS_STOP, 0, 0, 0, 0);
	v4l2_ctrl_new_std(handler, ops, V4L2_CID_AUTO_FOCUS_STATUS, 0, 7, 0, 0);
	v4l2_ctrl_new_std(handler, ops, V4L2_CID_FOCUS_AUTO, 0, 1, 1, 1);

	if (handler->error) {
		ret = handler->error;
		v4l2_ctrl_handler_free(handler);
	}

	sd->ctrl_handler = handler;

	return ret;
}

static int sensor_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	struct v4l2_subdev *sd;
	struct sensor_info *info;

	info = kzalloc(sizeof(struct sensor_info), GFP_KERNEL);
	if (info == NULL)
		return -ENOMEM;
	sd = &info->sd;
	cci_dev_probe_helper(sd, client, &sensor_ops, &cci_drv);
	sensor_init_controls(sd, &sensor_ctrl_ops);

	mutex_init(&info->lock);

	info->fmt = &sensor_formats[0];
	info->fmt_pt = &sensor_formats[0];
	info->win_pt = &sensor_win_sizes[0];
	info->fmt_num = N_FMTS;
	info->win_size_num = N_WIN_SIZES;
	info->sensor_field = V4L2_FIELD_NONE;
	info->stream_seq = MIPI_BEFORE_SENSOR;
	info->af_first_flag = 1;
	info->auto_focus = 0;
	return 0;
}

static int sensor_remove(struct i2c_client *client)
{
	struct v4l2_subdev *sd;

	sd = cci_dev_remove_helper(client, &cci_drv);
	pr_info("sensor_remove ov5640 sd = %p!\n", sd);
	kfree(to_state(sd));
	return 0;
}

static const struct i2c_device_id sensor_id[] = {
	{SENSOR_NAME, 0},
	{}
};

MODULE_DEVICE_TABLE(i2c, sensor_id);
static struct i2c_driver sensor_driver = {
	.driver = {
		   .owner = THIS_MODULE,
		   .name = SENSOR_NAME,
		   },
	.probe = sensor_probe,
	.remove = sensor_remove,
	.id_table = sensor_id,
};
static __init int init_sensor(void)
{
	return cci_dev_init_helper(&sensor_driver);
}

static __exit void exit_sensor(void)
{
	cci_dev_exit_helper(&sensor_driver);
}

module_init(init_sensor);
module_exit(exit_sensor);
